<?php

namespace App\Models\SerialNumber;

use App\Models\Cert\CertDriver;
use App\Models\Common\CommonModel;
use App\Models\User\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

class SerialNumber extends CommonModel
{
    use HasFactory;

    protected $table = 'serial_number';

    public function getRecords(array $conditions) {
        //dd($conditions);
        if(!isset($conditions['orderBy'])){
            $conditions['orderBy']['col'] = 'id';
            $conditions['orderBy']['sort'] = 'desc';
        }

        $records = $this->newQuery();
        if(isset($conditions['ids'])){
            if(is_array($conditions['ids'])){
                $conditions['ids'] = array_filter($conditions['ids']);
                //这一层判断必须有，当array_filter是一个空数组
                if(count($conditions['ids'])>0){
                    $records = count($conditions['ids'])>1?$records->whereIn('id',$conditions['ids']):
                        $records->where('id',Arr::first($conditions['ids']));
                }
            }
            else{
                $records = $records->where('id',$conditions['ids']);
            }
        }

        if(!empty($conditions['password'])){
            $records = $records->where('password',$conditions['password']);
        }
        if(!empty($conditions['phone'])){
            $records = $records->where('phone',$conditions['phone']);
        }
        if(!empty($conditions['name'])){
            $records = $records->where('name','like','%'.$conditions['name'].'%');
        }
        if(!empty($conditions['createdAt'])){
            $records = $records->where('created_at','>=',$conditions['createdAt'][0])
                ->where('created_at','<=',$conditions['createdAt'][1]);
        }
        if(isset($conditions['status'])){
            if(is_array($conditions['status'])){
                $conditions['status'] = array_filter($conditions['status']);
                //这一层判断必须有，当array_filter是一个空数组
                if(count($conditions['status'])>0){
                    $records = count($conditions['status'])>1?$records->whereIn('status',$conditions['status']):
                        $records->where('status',Arr::first($conditions['status']));
                }
            }
            else{
                if($conditions['status']!='all'){
                    $records = $records->where('status',$conditions['status']);
                }
            }
        }
        if(isset($conditions['type'])){
            if(is_array($conditions['type'])){
                $conditions['type'] = array_filter($conditions['type']);
                //这一层判断必须有，当array_filter是一个空数组
                if(count($conditions['type'])>0){
                    $records = count($conditions['type'])>1?$records->whereIn('type',$conditions['type']):
                        $records->where('type',Arr::first($conditions['type']));
                }
            }
            else{
                if($conditions['type']!='all'){
                    $records = $records->where('type',$conditions['type']);
                }
            }
        }
        if(!empty($conditions['serialNumber'])){
            $records = $records->where('serial_number',$conditions['serialNumber']);
        }

        if(!empty($conditions['activeTime'])){
            $records = $records->whereHas('getUserInfo', function ($q) use ($conditions) {
               $q->where('active_time','>=',$conditions['activeTime'][0])
                   ->where('active_time','<=',$conditions['activeTime'][1]);

            });
        }
        if(!empty($conditions['isBinding'])){
            if($conditions['isBinding']==1){
                $records = $records->where('user_id','!=',0);
            }elseif($conditions['isBinding']==2){
                $records = $records->where('user_id',0);
            }else{

            }
        }
        return $records->orderBy($conditions['orderBy']['col'],$conditions['orderBy']['sort']);
    }

    public function getDriverInfo() {
        return $this->hasOne(CertDriver::class,'user_id','user_id');
    }

    public function getUserInfo() {
        return $this->hasOne(User::class,'id','user_id');
    }

    public static function generateSerialNumber($type) {
        $num = 0;
        $mapSerialNumber = self::query()->pluck('id','serial_number')->toArray();
        while (true){
            $num = str_pad(mt_rand(0, 999999), 6, "0", STR_PAD_BOTH);
            if(Str::contains($num, 4)){
                continue;
            } else{
                $num = $type.$num;
                if(isset($mapSerialNumber[$num]))
                    continue;
                break;
            }
        }
        return $num;
    }
}
